package com.marvels.financial.loan.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.financial.funds.dao.IFundDao;
import com.marvels.financial.funds.dto.FundDTO;
import com.marvels.financial.loan.dto.McfeFundBankDto;
import com.marvels.financial.loan.dto.McfeFundLimitDto;
import com.marvels.financial.loan.service.IFundService;

/**
 * @author 毛小宁
 * @date 2018年4月10日
 */
@Service
public class FundServiceImpl implements IFundService {
	@Autowired
	private IFundDao fundDao;

	@Override
	public List<FundDTO> findFundDTO(FundDTO ptDto) throws MarvelsBusinessException {
		if (ptDto != null) {
			if (ptDto.getPageNum() != null && ptDto.getPageSize() != null) {
				PageHelper.startPage(ptDto.getPageNum(), ptDto.getPageSize());
			}
		}	
		return this.fundDao.select(ptDto);
	}

	@Override
	@Transactional
	public void addFund(FundDTO ptDto) throws MarvelsBusinessException {
		if(ptDto==null){
			throw new MarvelsBusinessException("传入资金源参数不能为空");
		}
		UserDTO userDTO=CacheUtils.getCacheUser();
		ptDto.setCreatedBy(userDTO.getUserCode());
		ptDto.setUpdatedBy(userDTO.getUserCode());
		if("delete".equals(ptDto.getJudge())){
			if(StringUtil.isNotEmpty(ptDto.getFundCode().trim())){
				fundDao.updateFundByFundCode(ptDto);
				return;
			}
		}
		if(StringUtil.isEmpty(ptDto.getFundName().trim())){
			throw new MarvelsBusinessException("资金源名称不能为空");
		}
		if(StringUtil.isEmpty(ptDto.getStatus().trim())){
			throw new MarvelsBusinessException("资金源状态不能为空");
		}
		if(StringUtil.isEmpty(ptDto.getFundSourceAttribute().trim())){
			throw new MarvelsBusinessException("资金源属性不能为空");
		}
		if(StringUtil.isEmpty(ptDto.getFundSourceMobile().trim())){
			throw new MarvelsBusinessException("法人手机号码不能为空");
		}
		if(StringUtil.isEmpty(ptDto.getFundSourceIcNo().trim())){
			throw new MarvelsBusinessException("法人身份证不能为空");
		}
		if(StringUtil.isEmpty(ptDto.getFundLoanType().trim())){
			throw new MarvelsBusinessException("放款方式不能为空");
		}
		if("add".equals(ptDto.getJudge())){
			ptDto.setFundCode(maxFundCode());//生成资金源code
			String fundName = ptDto.getFundName().trim();
			//通过资金源名称查询资金源是否已经存在
			FundDTO fundDTO = new FundDTO();
			fundDTO.setFundName(fundName);
			List<FundDTO> fundDTOList = fundDao.select(fundDTO);
			if(fundDTOList!=null&&fundDTOList.size()>0){
				throw new MarvelsBusinessException("资金源名称已经存在");
			}
			ptDto.setFundName(fundName);
			fundDao.insertFund(ptDto);
			addLimitBank(ptDto);
		}else if("edit".equals(ptDto.getJudge())){
			fundDao.updateFundByFundCode(ptDto);
			fundDao.updateBankByFundCode(ptDto);
			fundDao.updateLimitByFundCode(ptDto);
			addLimitBank(ptDto);
		}
	}
	
	/**添加资金源授权银行和限额
	 * @author 娄凯琦
	 * @date 2018年4月19日  
	 * @param ptDto
	 */
	@Transactional
	private void addLimitBank(FundDTO ptDto)  throws MarvelsBusinessException{
		List<McfeFundBankDto> mcfeFundBankDtoList=ptDto.getMcfeFundBankDtoList();
		if(mcfeFundBankDtoList!=null&&mcfeFundBankDtoList.size()==0){
			throw new MarvelsBusinessException("请选择资金源支持银行");
		}	
		UserDTO userDTO=CacheUtils.getCacheUser();
		for(McfeFundBankDto tmpMcfeFundBankDto:mcfeFundBankDtoList){
			tmpMcfeFundBankDto.setFundCode(ptDto.getFundCode());
			tmpMcfeFundBankDto.setUpdatedBy(userDTO.getUserCode());
			tmpMcfeFundBankDto.setCreatedBy(userDTO.getUserCode());
			tmpMcfeFundBankDto.setUpdatedDate(new Date());
			tmpMcfeFundBankDto.setCreatedDate(new Date());
		}
		fundDao.insertFundBank(mcfeFundBankDtoList);
		List<Integer> tenorList = null;
		McfeFundLimitDto mcfeFundLimitDto = ptDto.getMcfeFundLimitDto();
		List<McfeFundLimitDto> list = new ArrayList<McfeFundLimitDto>();
		//循环期限集合生成限额集合
		if(mcfeFundLimitDto!=null){
			tenorList = mcfeFundLimitDto.getTenorList();
			for(Integer tenor:tenorList){
				McfeFundLimitDto TmpMcfeFundLimitDto = new McfeFundLimitDto();
				TmpMcfeFundLimitDto.setAddMaxAmount(mcfeFundLimitDto.getAddMaxAmount());
				TmpMcfeFundLimitDto.setDayLimit(mcfeFundLimitDto.getDayLimit());
				TmpMcfeFundLimitDto.setDayResAmount(mcfeFundLimitDto.getDayResAmount());
				TmpMcfeFundLimitDto.setIsValid("0");
				TmpMcfeFundLimitDto.setMaxAmount(mcfeFundLimitDto.getMaxAmount());
				TmpMcfeFundLimitDto.setMinAmount(mcfeFundLimitDto.getMinAmount());
				TmpMcfeFundLimitDto.setTenor(tenor);
				TmpMcfeFundLimitDto.setFundCode(ptDto.getFundCode());
				list.add(TmpMcfeFundLimitDto);
			}
			if(list.size()>0){
				fundDao.insertFundLimit(list);
			}
		}
	}
	
	@Override
	public FundDTO queryBankAndLimit(FundDTO ptDto) throws MarvelsBusinessException {
		if(ptDto ==null){
			throw new MarvelsBusinessException("查询资金源参数对象为空");
		}
		FundDTO fundDTO = new FundDTO();
		List<McfeFundBankDto> bankList = fundDao.selectBankByFundCode(ptDto);
		fundDTO.setMcfeFundBankDtoList(bankList);
		List<McfeFundLimitDto> limitList = fundDao.selectLimitByFundCode(ptDto);
		
		McfeFundLimitDto retrunObject = null;
		
		if(null!=limitList && limitList.size()>0){
			retrunObject = limitList.get(0);
			List<Integer> tenorList = new ArrayList<Integer>();
			for(McfeFundLimitDto tmpLimit:limitList){
				tenorList.add(tmpLimit.getTenor());
			}
			retrunObject.setTenorList(tenorList);
		}
		fundDTO.setMcfeFundLimitDto(retrunObject);
		return fundDTO;
	}

	@Override
	public String maxFundCode() {
		String maxFundCode = fundDao.maxFundCode();
		if(maxFundCode == null){
			maxFundCode = "1";
		}
		return maxFundCode;
	}

	
}
